package com.traffic.action;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.traffic.base.BaseAction;
import com.traffic.domain.BaseBean;
import com.traffic.domain.User;
import com.traffic.domain.Vihicle;
import com.traffic.util.StrUtil;
@Controller
@Scope("prototype")
@SuppressWarnings("serial")
public class VihicleAction extends BaseAction<User>{
	/**
	 * 通过ajax请求，获取用户信息
	 * @throws Exception
	 */
    public void show() throws Exception {
    	 Long userId = Long.parseLong("1");
    	 //查询user对象
         User user = userService.findUserById(userId);
         //封装baseBean属性
         BaseBean baseBean = new BaseBean();
         baseBean.setCardNumber(user.getCardNumber());
         baseBean.setUserId(user.getId());
         baseBean.setName(user.getName());
         baseBean.setPhoneNumber(user.getPhoneNumber());
         Vihicle vihicle = user.getVihicle();
         //根据user查询出Lisence
         if(user.getVihicle()!=null){         
         vihicle = vihicleServcie.findByVihicleId(user.getLisence().getId());
         baseBean.setVihicleIdentifyNumber(vihicle.getIdentifyNumber());
         baseBean.setVihicleNumber(vihicle.getNumber());
         baseBean.setVihicleType(vihicle.getType());
         baseBean.setVihicleId(vihicle.getId());
         baseBean.setVihicleStatus(vihicle.getStatus()); 
         }
         StrUtil.sendJson(baseBean);
    }
    /**
	 * 通过ajax请求，获取用户信息
	 * @throws Exception
	 */
    public String modify() throws Exception {
        
    	Long userId = Long.parseLong(request("userId")); //用户ID
    	Long vihicleId = Long.parseLong(request("vihicleId"));
    	String name = request("name");//用户姓名
    	System.out.println();
    	String cardNumber = request("cardNumber");//身份证号
    	String phoneNumber = request("phoneNumber");//手机号
    	String vihicleType = request("vihicleType");
    	String vihicleNumber = request("vihicleNumber");
    	String vihicleIdentifyNumber = request("vihicleIdentifyNumber");
    	User user = userService.findUserById(userId);
    	Vihicle vihicle = vihicleServcie.findByVihicleId(vihicleId);
    	
    	user.setCardNumber(cardNumber);
    	user.setPhoneNumber(phoneNumber);
    	user.setName(name);
    	user.setStatus((long) 1);
    	user.setVihicle(vihicle);   	
    	userService.update(user);
    	vihicle.setIdentifyNumber(vihicleIdentifyNumber);
    	vihicle.setNumber(vihicleNumber);
    	vihicle.setType(vihicleType);
    	vihicle.setStatus((long) 1);
    	vihicleServcie.update(vihicle);
    	return "toSuccess";
    }
    /**
     * 转向主页面user.html
     * @return
     * @throws Exception
     */
    public String index() throws Exception {
   	     return "index";
    }
}
